package com.xyy.saas.payment.adpater.yzh.util;

import com.alibaba.fastjson.JSONObject;
import com.xyy.saas.payment.adpater.yzh.constant.ConfigPath;
import com.xyy.saas.payment.adpater.yzh.constant.DataDict;
import com.xyy.saas.payment.adpater.yzh.constant.XmlData;
import com.xyy.saas.payment.adpater.yzh.vo.BackNotifyData;
import com.xyy.saas.payment.adpater.yzh.vo.BankCardOrderResp;
import com.xyy.saas.payment.adpater.yzh.vo.WXPayOrderResp;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.util.ApolloUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.env.Environment;

import java.util.Map;

/**
 * @Author lina
 * @Description
 * @Date 2022/3/16
 **/
@Slf4j
public class YzhResultParseUtil {
    /**
     * 解析微信下单响应
     * @param result
     * @return
     */
    public static WXPayOrderResp parseWXPayApplyResult(Map<String, Object> result) {
        JSONObject dataJson = getData(result);

        if(dataJson == null) {
            return WXPayOrderResp.builder().code(ResultCodeEnum.DEFAULT_ERROR.getCode()).
                    failMsg(ResultCodeEnum.DEFAULT_ERROR.getDisplayMsg()).build();
        }

        String code = dataJson.getString("code");

        if(!DataDict.SUCCESS_CODE.equals(code)) {
            return WXPayOrderResp.builder().code(code).failMsg(dataJson.getString("message")).build();
        }

        String data = dataJson.getString("data");
        return JSONObject.parseObject(data, WXPayOrderResp.class);
    }

    /**
     * 解析同步调用云账户银行卡支付返回结果
     * @param result
     * @return
     */
    public static BankCardOrderResp parseBankPayApplyResult(Map<String, Object> result) {
        JSONObject dataJson = getData(result);
        String code = dataJson.getString("code");
        if (!DataDict.SUCCESS_CODE.equals(code)) {
            return BankCardOrderResp.builder().code(code).failMsg(dataJson.getString("message")).build();
        }
        String data = dataJson.getString("data");
        if (StringUtils.isBlank(data)) {
            return null;
        }
        return JSONObject.parseObject(data, BankCardOrderResp.class);
    }

    public static BackNotifyData parsePayQueryResult(Map<String, Object> result, String method) {
        JSONObject dataJson = getData(result);
        String data = dataJson.getString("data");
        Environment environment = ApolloUtil.getEnvironment();
        String[] activeProfiles = environment.getActiveProfiles();
        //线上环境，或者测试环境开启线上配置，将会解码返回数据
        if ("prod".equals(activeProfiles[0]) || "staging".equals(activeProfiles[0]) ||
                ApolloUtil.getProperty(ConfigPath.YZH_TEST_USE_PROD_ENV, Boolean.class))
        {
            try {
                data = DESUtil.decode(data, method);
            } catch (Exception e) {
                log.info("parseBankPayQueryResult:decode失败#data:{}", data);
            }
        }
        return JSONObject.parseObject(data, BackNotifyData.class);
    }

    private static JSONObject getData(Map<String, Object> result) {
        if (null == result) {
            return null;
        }
        Object data = result.get(XmlData.DATA);
        if (null == data) {
            return null;
        }
        return JSONObject.parseObject(data.toString());

    }
}
